mysql_host="127.0.0.1"
mysql_port=3306
database_name='testflow'
mysql_username='root'
mysql_password='123456'

DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
PARENTPATH=$(dirname ${DIR})
ROOTPATH=$(dirname ${PARENTPATH})
source ${ROOTPATH}/tools.sh

if [[ "127.0.0.1" == "${mysql_host}" ]]
then
  mysql_host=$(getIp)
fi
if [[ ! -d /opt/soft/docker/flowableui/conf ]]
then
  sudo mkdir -p /opt/soft/docker/flowableui/conf
fi
sudo cp ${PARENTPATH}/resource/flowableui.properties /opt/soft/docker/flowableui/conf
if [[ ! -d /opt/soft/docker/flowableui/lib ]]
then
  sudo mkdir -p /opt/soft/docker/flowableui/lib
fi
sudo cp ${PARENTPATH}/resource/mysql-connector-java-8.0.29.jar /opt/soft/docker/flowableui/lib/
name=`uname -s`
flag=''
if [[ "${name}" = "Linux" ]]; then
  flag=''
else
  flag='" "'
fi
sudo sed -i ${flag} 's/<mysql_host>/'"${mysql_host}"'/g'  /opt/soft/docker/flowableui/conf/flowableui.properties
sudo sed -i ${flag} 's/<mysql_port>/'"${mysql_port}"'/g'  /opt/soft/docker/flowableui/conf/flowableui.properties
sudo sed -i ${flag} 's/<database_name>/'"${database_name}"'/g'  /opt/soft/docker/flowableui/conf/flowableui.properties
sudo sed -i ${flag} 's/<mysql_username>/'"${mysql_username}"'/g'  /opt/soft/docker/flowableui/conf/flowableui.properties
sudo sed -i ${flag} 's/<mysql_password>/'"${mysql_password}"'/g'  /opt/soft/docker/flowableui/conf/flowableui.properties

mysql -h${mysql_host} -p${mysql_password} -u${mysql_username} -e 'show databases ;' | grep -E '^'"${database_name}"'$'

if [[ $? -eq 1 ]]
then
    mysql -h${mysql_host} -p${mysql_password} -u${mysql_username} -e 'create database '"${database_name}"''
fi
container_name="flowable-ui-mysql"
docker ps -a | grep -E '^'"${container_name}"'$'
if [[ $? -eq 1 ]]
then
    docker stop ${container_name} >/dev/null 2>&1
    docker rm -f ${container_name} >/dev/null 2>&1
fi
sudo docker run --name flowable-ui-mysql -p 7070:8080 \
  -v /opt/soft/docker/flowableui/lib/mysql-connector-java-8.0.29.jar:/app/WEB-INF/lib/mysql-connector-java-8.0.29.jar \
  -v /opt/soft/docker/flowableui/conf/flowableui.properties:/app/WEB-INF/classes/flowable-default.properties \
  -d --restart=always flowable/flowable-ui
